const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const OrderItem = sequelize.define('OrderItem', {
        id: {
            type: DataTypes.BIGINT,
            primaryKey: true,
            autoIncrement: true,
            comment: '明细ID'
        },
        order_id: {
            type: DataTypes.BIGINT,
            allowNull: false,
            references: {
                model: 'recycle_orders',
                key: 'id'
            }
        },
        category_id: {
            type: DataTypes.BIGINT,
            allowNull: false,
            references: {
                model: 'clothing_categories',
                key: 'id'
            }
        },
        weight: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false
        },
        points: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        deleted_at: DataTypes.DATE
    }, {
        paranoid: true,
        timestamps: true,
        tableName: 'order_items',
        comment: '订单明细表'
    });

    OrderItem.associate = function (models) {
        OrderItem.belongsTo(models.RecycleOrder, {
            foreignKey: 'order_id',
            as: 'order'
        });
        OrderItem.belongsTo(models.ClothingCategory, {
            foreignKey: 'category_id',
            as: 'category'
        });
    };

    return OrderItem;
};